package com.hxzy.config.security.authentication;

import cn.hutool.core.util.StrUtil;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.util.ServletUtils;
import com.hxzy.common.vo.R;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * spring security没有权限的自定义处理
 * 功能描述
 *
 * @author tonneyyy
 */
@Component
public class NewBeeAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        int code = HttpStatus.UNAUTHORIZED.value();
        //hutool 简称 糊图
        String msg = StrUtil.format( "请求访问：{}，认证失败，无法访问系统资源", request.getRequestURI());
        R r=R.build(AckCode.NO_PERMISSION_TO_ACCESS);
        r.setMsg(msg);
        ServletUtils.renderString(response,r );
    }
}
